﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;


namespace BusinessModule.Forms
{
    /// <summary>
    /// Main Form control container.
    /// </summary>
    [Serializable]
    public class FormControlContainer
    {

        #region Variables

        private BaseControl[,] ControlsMatrix;
        private int numOfColumns;
        private int numOfRows;

        /// <summary>
        /// Source model
        /// </summary>

        private MetaARMAdapter _source;

        #endregion

        #region Properties

        public MetaARMAdapter Source
        {
            set { _source = value; }
            get { return _source; }
        }

        #endregion

        #region Constructor


        /// <summary>
        /// Default contstructor
        /// </summary>
        public FormControlContainer(int rows, int cols)
        {
            ControlsMatrix = new BaseControl[rows, cols];
            numOfRows = rows;
            numOfColumns = cols;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Returns Control in matrix if exists, null otherwise
        /// </summary>
        /// <param name="x">x coord</param>
        /// <param name="y">y coord</param>
        /// <returns>control</returns>
        public BaseControl GetControl(int y, int x)
        {
            if (x >= 0 && x < numOfColumns && y >= 0 && y < numOfRows)
            {
                return ControlsMatrix[x, y];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Adds control to Container
        /// </summary>
        /// <param name="x">x coord</param>
        /// <param name="y">y coord </param>
        /// <param name="ctrl">Control to be added</param>
        public void AddControl(int x, int y, BaseControl ctrl)
        {
            if (x >= 0 && x < numOfColumns && y >= 0 && y < numOfRows)
            {
                ControlsMatrix[x, y] = ctrl;
                ControlsMatrix[x, y].Id = "jjj";
                //check if control should be mapped
                if (ControlsMatrix[x, y] is BaseMappedControl)
                {
                    ((BaseMappedControl)ControlsMatrix[x, y]).Source = this.Source;
                }
            }
        }

        /// <summary>
        /// Removes control from specifies position
        /// </summary>
        /// <param name="x"> x coord </param>
        /// <param name="y"> y coord </param>
        public void RemoveControl(int x, int y)
        {
            if (x >= 0 && x < numOfColumns && y >= 0 && y < numOfRows)
            {
                ControlsMatrix[x, y] = null;
            }
        }

        #endregion

        #region Properties

        public int NumOfRows
        {
            get { return numOfRows; }
        }

        public int NumOfColumns
        {
            get { return numOfColumns; }
        }

        #endregion

    }
}
